package com.atguigu.gulimall.product.app;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

@Controller
public class ExportExcle {
    @Autowired
    private CategoryService categoryService;

    @RequestMapping(value = "/categoryExcel", method = RequestMethod.POST)
    public void exportCompanyExcel(long catId, String name, HttpServletResponse response) throws Exception {
        try {
            List<CategoryEntity> categoryEntityList = categoryService.listWithTree();
            HSSFWorkbook wb = new HSSFWorkbook();

            HSSFSheet sheet = wb.createSheet("商品信息表");

            HSSFRow row = null;

            row = sheet.createRow(0);
            row.setHeight((short) (30 * 20));
            row.createCell(0).setCellValue("商品信息表");


            CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0, 2);
            sheet.addMergedRegion(rowRegion);
            row = sheet.createRow(1);
            row.setHeight((short) (22.50 * 20));//设置行高
            row.createCell(0).setCellValue("catId");//为第一个单元格设值
            row.createCell(1).setCellValue("name");//为第二个单元格设值
            /*row.createCell(2).setCellValue("委托方中文名称");//为第三个单元格设值
            row.createCell(3).setCellValue("委托方英文名称");
            row.createCell(4).setCellValue("法人代表");
            row.createCell(5).setCellValue("联系电话");
            row.createCell(6).setCellValue("企业地址");
            row.createCell(7).setCellValue("详细地址");
            row.createCell(8).setCellValue("一般纳税人");
            row.createCell(9).setCellValue("邮箱");
            row.createCell(10).setCellValue("网址");
            row.createCell(11).setCellValue("备注");
            row.createCell(12).setCellValue("经营范围");
            row.createCell(13).setCellValue("状态");
            row.createCell(14).setCellValue("审核状态");
            row.createCell(14).setCellValue("审核备注");*/

            for (int i = 0; i < categoryEntityList.size(); i++) {
                row = sheet.createRow(i + 2);
                CategoryEntity categoryEntity = categoryEntityList.get(i);
                row.createCell(0).setCellValue(categoryEntity.getCatId());
                row.createCell(1).setCellValue(categoryEntity.getName());
         /*       row.createCell(2).setCellValue(company.getNameCN());
                row.createCell(3).setCellValue(company.getNameEN());
                row.createCell(4).setCellValue(company.getContacts());
                row.createCell(5).setCellValue(company.getContactsPhone());
                row.createCell(6).setCellValue(company.getAddress());
                row.createCell(7).setCellValue(company.getAddressDetail());
                row.createCell(8).setCellValue(company.getCommonTaxpayer() == 1 ? "是" : "否");
                row.createCell(9).setCellValue(company.getEmail());
                row.createCell(10).setCellValue(company.getUrl());
                row.createCell(11).setCellValue(company.getRemark());
                row.createCell(12).setCellValue(company.getBusinessScopeIds());
                row.createCell(13).setCellValue(company.getStatus() == 1 ? "启用" : "停用");
                row.createCell(14).setCellValue(company.getAuditStatus() == 1);
                row.createCell(14).setCellValue(company.getAuditRemark());*/
            }
            sheet.setDefaultRowHeight((short) (16.5 * 20));
            //列宽自适应
            for (int i = 0; i <= 13; i++) {
                sheet.autoSizeColumn(i);
            }
            String title = "category";
            String fileName = new String(title.getBytes("GB2312"), "ISO_8859_1");
            fileName = URLEncoder.encode(fileName, "utf-8");

            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            OutputStream os = response.getOutputStream();
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");//默认Excel名称
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
